﻿Imports System.Management
Imports System.IO
Imports System.Threading
Imports System.Security.Principal
Imports OpenCLNet.OpenCL
Imports OpenHardwareMonitor
Imports OpenHardwareMonitor.Hardware
Public Class clsHWInfo
    Private ohmComp As New OpenHardwareMonitor.Hardware.Computer
    Private WithEvents tClock As System.Timers.Timer

    Public Structure sGpuInfo
        Public CoreLoad
        Public MemoryControllerLoad
        Public Temperature
        Public FanSpeed
        Public MemoryUsage
        Public CoreClock
        Public RamClock
        Public ShadersClock
    End Structure
    Private _Ginf() As sGpuInfo
    Public ReadOnly Property GPU_Count As Int16
        Get
            Return _Ginf.Count
        End Get
    End Property
    Public Function Gpu_Inf(ByVal Index) As sGpuInfo
        Return _Ginf(Index)
    End Function

    Public Structure sCpuInfo
        Public Structure sCoreInfo
            Public Temperature As Int32
            Public Load As Int32
            Public Clock As Int32
        End Structure
        Private _Core() As sCoreInfo
        Public CoreCount As Int32
        Public TotalLoad As Int32
        Public ReadOnly Property AvgTemp
            Get
                Try
                    Dim iTotal As Int32 = 0
                    For Each dCore As sCoreInfo In _Core
                        iTotal += dCore.Temperature
                    Next
                    Return (iTotal / _Core.Count)
                Catch ex As Exception
                    Return 0
                End Try
            End Get
        End Property
        Public ReadOnly Property AvgLoad
            Get
                Try
                    Dim iTotal As Int32 = 0
                    For Each dCore As sCoreInfo In _Core
                        iTotal += dCore.Load
                    Next
                    Return (iTotal / _Core.Count)
                Catch ex As Exception
                    Return 0
                End Try
            End Get
        End Property
    End Structure
    Private _Cinf() As sCpuInfo
    Public ReadOnly Property CPU_Count
        Get
            Return _Cinf.Count
        End Get
    End Property
    Public ReadOnly Property CPU_Inf(ByVal Index As Int16) As sCpuInfo
        Get
            Return _Cinf(Index)
        End Get
    End Property

    Public Event HardWareAdded(ByVal hw As IHardware)
    Public Event HardwareRemoved(ByVal hw As IHardware)

    Private Sub hwAdded(ByVal hw As Hardware.IHardware)
        RaiseEvent HardWareAdded(hw)
    End Sub
    Private Sub hwRemoved(ByVal hw As IHardware)
        RaiseEvent HardwareRemoved(hw)
    End Sub

   

    Public Function InitOHM() As Boolean
        Try
            AddHandler ohmComp.HardwareAdded, AddressOf hwAdded
            AddHandler ohmComp.HardwareRemoved, AddressOf hwRemoved
            ohmComp.Open()
            LogWindow.WriteLog("OpenHardware monitor initialized succesfully")
            Return True
        Catch ex As Exception
            LogWindow.WriteError("HWinf_InitOHM", Err)
        End Try
    End Function



    Private _X64 As Boolean, _intCpuCount As Int16, _intCpuCoresTotal As Int16, _strCpuName As String, _strOSName As String, _OsSupported As Boolean
    Private _strUserName As String, _IsAdmin As Boolean, _HasAffintyChanger As Boolean
    Public Enum eWindowsPlatform
        Unsupported = 0
        WindowsXPsp3 = 1
        Windows2003 = 2
        WindowsVista = 3
        Windows2008 = 4
        Windows7 = 5
    End Enum
    Private _WinPlatform As eWindowsPlatform = eWindowsPlatform.Unsupported
    Public ReadOnly Property WindowsPlatform() As eWindowsPlatform
        Get
            Return _WinPlatform
        End Get
    End Property
    Private _Password As String = vbNullString
    Public Property AccPassword() As String
        Get
            Return _Password
        End Get
        Set(ByVal value As String)
            _Password = value
        End Set
    End Property
    Private _ht As Boolean = False
    Public ReadOnly Property HasHT() As Boolean
        Get
            Return _ht
        End Get
    End Property
    Public ReadOnly Property IsAdmin() As Boolean
        Get
            Return _IsAdmin
        End Get
    End Property
    Public ReadOnly Property IsX64() As Boolean
        Get
            Return _X64
        End Get
    End Property
    Public ReadOnly Property OsName() As String
        Get
            Return Environment.OSVersion.VersionString
        End Get
    End Property
    Public ReadOnly Property TotalCores() As Int16
        Get
            Dim iTotal As Int16 = 0
            For Each sCpu As InstalledCPU In _colCpus
                iTotal += sCpu.NumberOfCores
            Next
            Return iTotal
        End Get
    End Property
    Public ReadOnly Property InstalledCPUs() As Int16
        Get
            Return _intCpuCount
        End Get
    End Property
    <Serializable()> _
    Public Structure InstalledGPU
        Public Enum eVendor
            Nvidia
            Ati
        End Enum
        Public Vendor As eVendor
        Public DeviceName As String
        Public AdapterRam As String
        Public DriverVersion As String
        Public ClockSpeed As String
        Public ECC As Boolean
    End Structure
    <Serializable()> _
    Public Structure InstalledCPU
        Public Name As String
        Public Manufacturer As String
        Public ClockSpeed As String
        Public NumberOfCores As Int16
        Public NumberOfLogicalCpus As Int16
        Public ReadOnly Property HasHT() As Boolean
            Get
                If NumberOfLogicalCpus <> NumberOfCores Then
                    Return True
                Else
                    Return False
                End If
            End Get
        End Property
    End Structure
    Private _colCpus As New Collection
    Private _colGpus As New Collection
    Public ReadOnly Property GetCpu(ByVal Index As Int16) As InstalledCPU
        Get
            Return _colCpus(Index)
        End Get
    End Property
    Public ReadOnly Property GetGpu(ByVal Index As Int16) As InstalledGPU
        Get
            Return _colGpus(Index)
        End Get
    End Property
    Public Function FillCpuInfo() As Boolean
        Try
            Dim query As ObjectQuery, searcher As ManagementObjectSearcher, queryCollection As ManagementObjectCollection
            query = New ObjectQuery("SELECT * FROM Win32_Processor")
            searcher = New ManagementObjectSearcher(query)
            queryCollection = searcher.Get()
            For Each m As ManagementObject In queryCollection
                Dim nCpu As New InstalledCPU
                With nCpu
                    .NumberOfCores = m.GetPropertyValue("NumberOfCores")
                    _intCpuCoresTotal += CInt(.NumberOfCores)
                    _intCpuCount += 1
                    .NumberOfLogicalCpus = m.GetPropertyValue("NumberOfLogicalProcessors")
                    .Name = m.GetPropertyValue("Name")
                    .Manufacturer = m.GetPropertyValue("Manufacturer")
                    .ClockSpeed = m.GetPropertyValue("CurrentClockSpeed")
                    If .NumberOfLogicalCpus <> .NumberOfCores Then _ht = True
                End With
                _colCpus.Add(nCpu)
            Next
            Return True
        Catch ex As Exception
            LogWindow.WriteError("HWinfo_WMICPU", Err)
            LogWindow.WriteLog("Trying registry method")
            Dim rKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("HARDWARE\DESCRIPTION\System\CentralProcessor")
            Dim sKey() As String = rKey.GetSubKeyNames
            If sKey.GetUpperBound(0) = 0 Then
                LogWindow.WriteLog("Could not get cpu info from registry")
                Return False
            Else
                Try
                    Dim vKey As Microsoft.Win32.RegistryKey
                    vKey = rKey.OpenSubKey(sKey(0))

                    Dim nCpu As New InstalledCPU
                    With nCpu
                        .Name = vKey.GetValue("ProcessorNameString")
                        .ClockSpeed = vKey.GetValue("~MHz")
                        .Manufacturer = vKey.GetValue("VendorIdentifier")
                        .NumberOfCores = sKey.Count
                    End With
                    _colCpus.Add(nCpu)
                    Return True
                Catch ex1 As Exception
                    LogWindow.WriteError("HWinfo_RegistryCPU", Err)
                    Return False
                End Try
            End If
        End Try
    End Function
    Public Function Is2kHotFix() As Boolean
        Try
            ' select * from Win32_QuickFixEngineering where hotfixid = 982861
        Catch ex As Exception

        End Try
    End Function
    Public Function FillOsInfo() As Boolean
        Try
            If Environment.OSVersion.Version.Major < 5 Or (Environment.OSVersion.Version.Major = 5 And Environment.OSVersion.Version.Minor = 0) Then
                _WinPlatform = eWindowsPlatform.Unsupported
            ElseIf Environment.OSVersion.Version.Major = 5 Then
                Select Case Environment.OSVersion.Version.Minor
                    Case 0I
                        _WinPlatform = eWindowsPlatform.Unsupported
                    Case 1I
                        If Environment.OSVersion.ServicePack < 3 Then
                            _WinPlatform = eWindowsPlatform.Unsupported
                        Else
                            _WinPlatform = eWindowsPlatform.WindowsXPsp3
                        End If
                    Case 2I
                        _WinPlatform = eWindowsPlatform.Windows2003
                End Select
            ElseIf Environment.OSVersion.Version.Major = 6 Then
                Select Case Environment.OSVersion.Version.Minor
                    Case 0I
                        _WinPlatform = eWindowsPlatform.WindowsVista
                    Case 1I
                        _WinPlatform = eWindowsPlatform.Windows2008
                    Case 2I
                        _WinPlatform = eWindowsPlatform.Windows7
                End Select
            Else
                LogWindow.WriteLog("Could not identify platform from the following version object: " & Environment.OSVersion.Version.Major & "." & Environment.OSVersion.Version.MajorRevision & "." & Environment.OSVersion.Version.Minor & "." & Environment.OSVersion.Version.MinorRevision & "." & Environment.OSVersion.Version.Revision & "." & Environment.OSVersion.Version.Build)
                _WinPlatform = eWindowsPlatform.Unsupported
            End If
            Return True
        Catch ex As Exception
            LogWindow.WriteError("HWinfo_FillOSinfo", Err)
            Return False
        End Try
    End Function
    Public Function FillIsX64() As Boolean
        Try
            If IntPtr.Size = 8 Then
                'win64
                _X64 = True
            ElseIf IntPtr.Size = 4 Then
                'win32
                _X64 = False
            End If
        Catch ex As Exception
            LogWindow.WriteError("HWinfo_IsX64", Err)
            _X64 = False
            Return False
        End Try
    End Function
    Public Function FillIsAdmin() As Boolean
        Try
            Dim mIdentity As System.Security.Principal.WindowsIdentity = WindowsIdentity.GetCurrent()
            _strUserName = mIdentity.Name
            Dim mPrincipal = New WindowsPrincipal(mIdentity)
            If mPrincipal.IsInRole(WindowsBuiltInRole.Administrator) Then
                _IsAdmin = True
            Else
                _IsAdmin = False
            End If
            Return True
        Catch ex As Exception
            LogWindow.WriteLog("Could not check account details")
            _IsAdmin = False
            Return False
        End Try
    End Function
    Public Function FillGpuInfo() As Boolean
        Try
            For Each oclPlatform As OpenCLNet.Platform In OpenCLNet.OpenCL.GetPlatforms
                Dim devices() As OpenCLNet.Device
                devices = oclPlatform.QueryDevices(OpenCLNet.DeviceType.GPU)
                For Each gpu As OpenCLNet.Device In devices
                    If gpu.Vendor.ToUpper.Contains("NVIDIA") Or gpu.Vendor.ToUpper.Contains("ATI") Or gpu.Vendor.ToUpper.Contains("AMD") Then
                        Dim nGpu As New InstalledGPU
                        With nGpu
                            .AdapterRam = gpu.GlobalMemSize / 1024 / 1024
                            .ClockSpeed = gpu.MaxClockFrequency
                            .DeviceName = gpu.Name
                            .DriverVersion = gpu.DriverVersion
                            .Vendor = gpu.Vendor
                            .ECC = gpu.ErrorCorrectionSupport
                        End With
                        _colGpus.Add(nGpu)
                    End If
                Next
            Next
            Return True
        Catch ex As Exception
            LogWindow.WriteError("HWinfo_FillGpus", Err)
            Return False
        End Try
    End Function
End Class
